<?php 
$req_path="./";
include_once($req_path."include/ini.php");
include_once($req_path."include/db.php");

class M_creditsale extends DB{
	private $dbConn;
	private $rowCount="0";
	private $totalpage="0";
	private $csid="0";
	function M_creditsale(){
		parent::__construct();
		//$this->dbConn=$this->getDB();
	}
 
	function getListPage(){
		global $list_page;
		return $list_page;
	}
	
	function getPrintData($id){
		$sqlh= "SELECT b.BranchName 
				, DATE_FORMAT(NOW(), '%d/%m/%Y %H:%i:%s') as SaleDate
				,Tax, b.Address 
				From tb_branch b 
				Where b.ID=".$_SESSION['bid'];
		$result = $this->ExecuteData($sqlh);
		$header=$result->FetchRow();
		
		$billNo=$this->getLastBillNo($_SESSION['bid']);
		$header['BillNo'] = $billNo;
		
		$sql= "SELECT b.RemainAmt, c.FirstName, c.LastName
				From tb_creditsale b
				Left join tb_customer c on c.ID=b.CustID
				Where b.IsDeleted = 0  and b.id=".$id;
				
		$result = $this->ExecuteData($sql);
	 
		$ar_result= Array();
		$row=$result->FetchRow();
 
		$header['FirstName'] = $row['FirstName'];
		$header['LastName'] = $row['LastName'];
		$header['RemainAmt'] = $row['RemainAmt'];
		
		return $header;//$ar_result;		
	}
	
	function getLastBillNo($bid){
		$sql ="select BillNo from tb_branch where id='".$bid."'";
		
		$result = $this->ExecuteData($sql);
		
		$sqlupdate="update tb_branch set BillNo=BillNo+1 where id='".$bid."'";
		$this->ExecuteData($sqlupdate);
		
		$row=$result->FetchRow();
		
		return $row["BillNo"]+1;
	}
	
	function getData($code=null, $name=null, $status=0, $bid=0, $pageStart=1){
		global $list_page;
		
		$sqlscript= "select cs.id, c.Code, c.FirstName , c.LastName  "
					." ,DATE_FORMAT( cs.Buydate, '%d/%m/%Y' ) _buydate"
					." ,cs.TotalAmt ,cs.PayAmt ,cs.RemainAmt"
					." ,DATE_FORMAT( cs.LastPay, '%d/%m/%Y' ) lastpay "
					." , b.BranchName "
					." from tb_creditsale cs "
					." join tb_customer c on c.ID=cs.CustID  "
					." join tb_branch b on b.BranchCode = LEFT(c.Code,4)  "
					." where  cs.IsDeleted = 0  and 1=1 and b.IsActived=1 and cs.IsDeleted = 0 ";
					
		 if ($bid!="0"){
			$sqlscript.= ' and b.ID='.$bid;
		}
		
		if (isset($code) && $code!=""){
			$sqlscript.= " and c.Code='". $code."'";
		}
		if (isset($name) && $name!=""){
			$sqlscript.= " and CONCAT(c.FirstName ,' ',  c.LastName ) like '%". $name."%' ";
		}
		if (isset($status) && $status !=0){
			if ($status==1){
				$sqlscript.=  " and RemainAmt=0";
			}else if ($status==2){
				$sqlscript.=  " and RemainAmt > 0 and DATEDIFF( Now() , Buydate) <= 30 ";	
			}else if ($status==3){
				$sqlscript.=  " and RemainAmt > 0 and DATEDIFF( Now() , Buydate) > 30 ";	
			}
		}
		//Order 
		$sqlscript.= " order by Buydate Desc " ;
	 
		$re = $this->ExecuteData($sqlscript);
		$rows =$re->RecordCount();
		
		//Page limit
		$sqlscript.= " limit ". ($pageStart-1)*$list_page. ", " . $list_page;
		
		//paging
		$result = $this->ExecuteData($sqlscript);
		 
		$totalpage=1;
		if ($rows>0){
			$rt = $rows%$list_page;
			
			if($rt!=0) {
				$totalpage = floor($rows/$list_page)+1;
			}
			else {
				$totalpage = floor($rows/$list_page);
			}
		}
		
		$this->rowCount=$rows;
		$this->totalpage=$totalpage;
		return $result;
	}
	
	function getDataDetail($id=0){
		$sqlscript= " SELECT id   ";
		$sqlscript.=" , DATE_FORMAT( PayDate, '%d/%m/%Y' ) PayDate2, Amount ";
		$sqlscript.=" , Case when PayType=1 Then 'Cash' when PayType=2 then 'Credit Card' Else 'Smart Purse' end PayType ";
		$sqlscript.=" , CardNo ";
		$sqlscript.= " From tb_creditsale_detail ";
		$sqlscript.= " where Credit_ID='".$id."'";
		$sqlscript.= " order by PayDate desc ";
		
		$result = $this->ExecuteData($sqlscript);
		$this->rowCount=$result->RecordCount();
		return $result;
	}

	function getRowCount(){
		return $this->rowCount;
	}
	
	function getLastInsert(){
		return $this->csid;
	}
	
	function InsertCredit($val){
		if (isset($val)){
			$sqlInsert=$this->InsertSqlScript("select * from tb_creditsale_detail where 1=2", $val);
			$this->ExecuteData($sqlInsert);
			$this->csid = $this->getLastID();
			return $this->csid;
		}
	}
	function updateCreditAmt($id,$amt,$user){
			
			$sql= " update tb_creditsale set LastPay=Now(), PayAmt=PayAmt+ ".$amt." ,  RemainAmt=RemainAmt-".$amt.",UpdatedDate=Now(), UpdatedBy='".$user."' where id=".$id;
			$this->ExecuteData($sql);
 		
	}
	function updateCreditAmt_reduce($id,$amt,$user){
			
			$sql= " update tb_creditsale set PayAmt=PayAmt- ".$amt." ,  RemainAmt=RemainAmt+".$amt.",UpdatedDate=Now(), UpdatedBy='".$user."' where id=".$id;
			$this->ExecuteData($sql);
 		
	}
	function deleCredit($id){
		if(isset($id)){
			$sqlDelete = "delete from tb_creditsale_detail where id='".$id."'";
			$this->ExecuteData($sqlDelete);
			return "c";
		}
	}
	function getTotalPage(){
		return $this->totalpage;
	 
	}
}
?>